package max_area_of_island

var dirs = [][2]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}

func maxAreaOfIsland(grid [][]int) (res int) {
	m, n := len(grid), len(grid[0])
	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			t := dfs(grid, i, j)
			res = max(res, t)
		}
	}
	return res
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func dfs(grid [][]int, i, j int) (cnt int) {
	m, n := len(grid), len(grid[0])
	if i < 0 || i >= m || j < 0 || j >= n {
		return 0
	}
	if 0 == grid[i][j] {
		return 0
	}
	grid[i][j] = 0
	for index := 0; index < len(dirs); index++ {
		nextI := i + dirs[index][0]
		nextJ := j + dirs[index][1]
		cnt += dfs(grid, nextI, nextJ)
	}
	cnt++
	return
}
